package br.edu.com1263.dao;

import java.util.Collection;

import javax.persistence.Query;

import br.edu.com1263.model.Professor;

public class ProfessorDAOImpl extends AbstractDAO implements ProfessorDAO{
	
	public ProfessorDAOImpl(){

}

	@Override
	public Professor consultar(Long id) {
		return manager.find(Professor.class, id);
	}

	@Override
	public Collection<Professor> consultarTodos() {
		Query query = manager.createQuery("SELECT p FROM Professor p");
		return query.getResultList();
	}

	@Override
	public void salvar(Professor professor) {
		manager.persist(professor);
		
	}

	@Override
	public void alterar(Professor professor) {
		manager.merge(professor);
		
	}

	@Override
	public void remover(Professor professor) {
		manager.remove(professor);
		
	}

	@Override
	public Professor validar(String matricula, String senha) {
		Query query = manager.createQuery("SELECT p FROM Professor p Where p.matricula = :matricula AND p.senha = :senha");
		query.setParameter("matricula", matricula);
		query.setParameter("senha", senha);
		Collection<Professor> professor = query.getResultList();
		if (professor.size() == 1)
			return professor.iterator().next();
		return null;
	}

	@Override
	public Professor consultarMatricula(String matProfessor) {
		Query query = manager.createQuery("SELECT p FROM Professor p Where p.matricula = :matricula");
		query.setParameter("matricula", matProfessor);
	
		return (Professor) query.getSingleResult();
	}
}
